package com.shareware.common.memory.entity

import android.app.ActivityManager
import com.shareware.common.memory.MemoryHelper
import com.shareware.common.utils.safeLet
import org.json.JSONObject

/**
 * desc: 功能描述
 * email: mobiledeveloper@qq.com
 */
data class SystemInfo(
    val totalMemByte: Long = -1,
    val availMemByte: Long = -1,
    val lowMemory: Boolean = false,
    val thresholdByte: Long = -1,
) {
    companion object {
        fun get(): SystemInfo {
            val info = ActivityManager.MemoryInfo()
            MemoryHelper.activityManager.getMemoryInfo(info)
            return SystemInfo(
                totalMemByte = info.totalMem,
                availMemByte = info.availMem,
                lowMemory = info.lowMemory,
                thresholdByte = info.threshold
            )
        }
    }

    override fun toString(): String {
        return String.format(
            "%-21s %-21s %-21s %-21s",
            "totalMem=$totalMemByte B",
            "availMem=$availMemByte B",
            "lowMemory=$lowMemory",
            "threshold=$thresholdByte B"
        )
    }

    fun toJson() = safeLet(defVal = JSONObject()) {
        JSONObject().apply {
            put("totalMemByte", totalMemByte)
            put("availMemByte", availMemByte)
            put("lowMem", lowMemory)
            put("threshold", thresholdByte)
        }
    }
}